<?php
/**[广告列表]
 * @Author: 976123967@qq.com
 * @Date:   2014-08-16 11:36:58
 * @Last Modified by:   chen
 * @Last Modified time: 2014-10-28 22:29:37
 */
class AdAction extends PublicAction{

	public function _initialize()
	{
		parent::_initialize();
		$this->model = D('Ad');
		// 位置分类
        $position = D('Position')->get_data();
        $this->assign('position',$position);
	}

	/**
	 * [index 广告列表]get_data();
	 * @return [type] [description]
	 */
	public function index()
	{
		// 数据
		$data = D('AdView')->get_data();
		// 分页
		if($data)
		{

			$this->assign('page',$data['page']);
			unset($data['page']);
		}
		// 分配
		$this->assign('data',$data);
		$this->display();
	}

	/**
	 * [add 添加]
	 */
	public function add()
	{
		if(IS_POST)
		{
			$psid = I('post.position_psid');
			$url = U('Ad/index',array('psid'=>$psid));
			// 验证
			if(!$this->model->create()) $this->error($this->model->getError());
			// 添加
			if(!$this->model->add_ad()) $this->error($this->model->getError());
			
			$this->success('广告信息添加成功',$url);
			die;
		}

	

		$this->display();
	}

	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{

		if(IS_POST)
		{
			$psid = I('post.position_psid');
			$url = U('Ad/index',array('psid'=>$psid));
			if(!$this->model->create()) $this->error($this->model->getError());
			if(!$this->model->save_ad()) $this->error($this->model->getError());
			$this->success('广告信息更新成功',$url);
			die;
		}

		$aid = I('get.aid');
		if(!$aid) _404('信息错误',U('Index/welcome'));
		$field = $this->model->find_one($aid);
		$this->assign('field',$field);
		
		

		$this->display();
	}


	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{
		$aid = I('get.aid');
        if(!$aid) _404('信息错误',U('Index/welcome'));
        $this->model->del_ad($aid);
        $psid = I('get.psid');
        $this->success('删除广告成功',U('Ad/index',array('psid'=>$psid)));
	}


	/**
	 * [update 更新信息]
	 * @return [type] [description]
	 */
	public function update()
	{
		$url =U('Ad/index');
		//执行操作
		switch (true) 
		{
			case isset($_POST['order']):
				if(!$this->model->update_order())
					$this->error($this->model->getError(),$url);
				$this->success('排序成功',$url);
				break;
			case isset($_POST['check']):
				if(!$this->model->update_check(2))
					$this->error($this->model->getError(),$url);
				$this->success('审核成功',$url);
				break;
			case isset($_POST['cancel_check']):
				if(!$this->model->update_check(1))
					$this->error($this->model->getError(),$url);
				$this->success('取消审核成功',$url);
				break;
			case isset($_POST['del_check']):
				if(!$this->model->delete_check())
					$this->error($this->model->getError(),$url);
				$this->success('删除成功',$url);
				break;
			default:
				$this->error('动作选择错误',$url);
		}
	}

	
}